<style>
    .table > tbody > tr > td > label.asterisk:after {
        content: " *";
        color: red;
    }
    .form-copy {
        width: 200px;
        margin-right: 5px;
        border-radius: 0;
        box-shadow: none;
        padding: 4px 12px 7px 12px;
        font-size: 14px;
        line-height: 1.42857143;
        color: #555;
        border: 1px solid #ccc;
        transition: border-color ease-in-out .15s,box-shadow ease-in-out .15s;
    }
    .select2 {
        margin-right: 5px
    }
</style>
<div class="box box-solid no-margin box-show">
    <div class="box-body">
        <h6 style="font-weight: bolder">基本信息</h6>
        <div class="box-body table-responsive no-padding">
            <table class="table">
                <tbody>
                <tr>
                    <td style="text-align: right;width: 100px;">
                        <label class="asterisk" style="padding-top: 8px;">返款类型</label>
                    </td>
                    <td style="text-align: left;width: 200px;">
                        <select id="paid_type" name="paid_type">
                            {{ getOptions(\App\Models\Reimburse\ReimburseApply::$_paidType) }}
                        </select>
                    </td>
                    <td style="text-align: right;width: 100px;">
                        <label class="asterisk" style="padding-top: 8px;">国家</label>
                    </td>
                    <td style="text-align: left;width: 200px;">
                        <select id="country_id" name="country_id">
                            {{ getOptions(\App\Models\Basics\AmazonCountry::options()) }}
                        </select>
                    </td>
                    <td style="text-align: right;width: 100px;">
                        <label class="asterisk" style="padding-top: 8px;" >费用类型</label>
                    </td>
                    <td style="text-align: left;">
                        <select id="fee_type" name="fee_type">
                            {{ getOptions(\App\Models\Reimburse\ReimburseApply::$_feeType) }}
                        </select>
                    </td>
                </tr>
                </tbody>
            </table>
            <table class="table">
                <tbody>
                <tr>
                    <td style="text-align: right;width: 100px;">
                        <label style="padding-top: 8px;">刷单类型</label>
                    </td>
                    <td style="text-align: left;width: 500px;">
                        <select id="r_type" name="r_type">
                            {{ getOptions(\App\Models\Reimburse\ReimburseApply::$_rType) }}
                        </select>
                        <span style="margin-top: 5px;margin-bottom: 10px;color: #737373;"><i class="fa fa-info-circle"></i>&nbsp;若费用类型为刷单，请务必选择刷单类型</span>
                    </td>
                    <td></td>
                    <td></td>
                    <td></td>
                </tr>
                </tbody>
            </table>
            <table class="table">
                <tbody>
                <tr>
                    <td style="text-align: right;width: 100px;">
                        <label style="padding-top: 8px;">订单号</label>
                    </td>
                    <td style="text-align: left;width: 500px;">
                        <input type="text" class="form-copy" name="order_no" placeholder="订单号">
                        <span style="margin-top: 5px;margin-bottom: 10px;color: #737373;"><i class="fa fa-info-circle"></i>&nbsp;若有订单号，请务必输入订单号</span>
                    </td>
                    <td></td>
                    <td></td>
                    <td></td>
                </tr>
                </tbody>
            </table>
            <table class="table">
                <tbody>
                <tr>
                    <td style="text-align: right;width: 100px;">
                        <label style="padding-top: 8px;">亚马逊SKU</label>
                    </td>
                    <td style="text-align: left;width: 200px;">
                        <input type="text" class="form-copy" name="seller_sku" placeholder="亚马逊SKU">
                    </td>
                    <td style="text-align: right;width: 100px;">
                        <label style="padding-top: 8px;">ASIN</label>
                    </td>
                    <td style="text-align: left;width: 200px;">
                        <input type="text" class="form-copy" name="asin" placeholder="ASIN">
                    </td>
                    <td></td>
                    <td></td>
                </tr>
                </tbody>
            </table>
            <table class="table">
                <tbody>
                <tr>
                    <td style="text-align: right;width: 100px;">
                        <label style="padding-top: 8px;">订单联系人</label>
                    </td>
                    <td style="text-align: left;">
                        <input type="text" class="form-copy" style="width: 520px;" name="order_atten" placeholder="订单联系人">
                    </td>
                </tr>
                </tbody>
            </table>
            <table class="table">
                <tbody>
                <tr>
                    <td style="text-align: right;width: 100px;">
                        <label style="padding-top: 8px;">备注</label>
                    </td>
                    <td style="text-align: left;">
                        <input type="text" class="form-copy" style="width: 520px;" name="remarks" placeholder="备注">
                    </td>
                </tr>
                </tbody>
            </table>
        </div>
    </div>
</div>
<div class="box box-solid no-margin box-show">
    <div class="box-body">
        <h6 style="font-weight: bolder">返款支付信息</h6>
        <div class="box-body table-responsive no-padding">
            <table class="table">
                <tbody>
                <tr>
                    <td style="text-align: right;width: 100px;">
                        <label class="asterisk" style="padding-top: 8px;">币种</label>
                    </td>
                    <td style="text-align: left;width: 200px;">
                        <select id="currency_code" name="currency_code">
                            {{ getOptions(\App\Models\Basics\Currency::options()) }}
                        </select>
                    </td>
                    <td style="text-align: right;width: 100px;">
                        <label class="asterisk" style="padding-top: 8px;">支付方式</label>
                    </td>
                    <td style="text-align: left;width: 200px;">
                        <select id="paid_method" name="paid_method">
                            {{ getOptions(\App\Models\Reimburse\ReimburseApply::$_paidMethod) }}
                        </select>
                    </td>
                    <td style="text-align: right;width: 100px;">
                        <label class="asterisk" style="padding-top: 8px;" >支付账号</label>
                    </td>
                    <td style="text-align: left;">
                        <input type="text" class="form-copy" name="paid_account_no" placeholder="支付账号">
                    </td>
                </tr>
                </tbody>
            </table>
            <table class="table">
                <tbody>
                <tr>
                    <td style="text-align: right;width: 100px;">
                        <label style="padding-top: 8px;">一次性付款</label>
                    </td>
                    <td style="text-align: left;">
                        <input type="text" class="form-copy paid_amount_" name="paid_amount_1" placeholder="一次性付款">
                    </td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                </tr>
                </tbody>
            </table>
            <table class="table">
                <tbody>
                <tr>
                    <td style="text-align: right;width: 100px;">
                        <label style="padding-top: 8px;" >第一次付款</label>
                    </td>
                    <td style="text-align: left;width: 200px;">
                        <input type="text" class="form-copy paid_amount_" name="paid_amount_2" placeholder="第一次付款">
                    </td>
                    <td style="text-align: right;width: 100px;">
                        <label style="padding-top: 8px;">第二次付款</label>
                    </td>
                    <td style="text-align: left;">
                        <input type="text" class="form-copy paid_amount_" name="paid_amount_3" placeholder="第二次付款">
                    </td>
                    <td></td>
                    <td></td>
                </tr>
                </tbody>
            </table>
            <table class="table">
                <tbody>
                <tr>
                    <td style="text-align: right;width: 100px;">
                        <label style="padding-top: 8px;">佣金</label>
                    </td>
                    <td style="text-align: left;">
                        <input type="text" class="form-copy paid_amount_" name="paid_amount_4" placeholder="佣金">
                    </td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                </tr>
                </tbody>
            </table>
            <table class="table">
                <tbody>
                <tr>
                    <td style="text-align: right;width: 100px;">
                        <label style="padding-top: 8px;">佣金币种</label>
                    </td>
                    <td style="text-align: left;width: 200px;">
                        <select id="currency_code_4" name="currency_code_4">
                            {{ getOptions(\App\Models\Basics\Currency::options()) }}
                        </select>
                    </td>
                    <td style="text-align: right;width: 100px;">
                        <label style="padding-top: 8px;">支付方式</label>
                    </td>
                    <td style="text-align: left;width: 200px;">
                        <select id="paid_method_4" name="paid_method_4">
                            {{ getOptions(\App\Models\Reimburse\ReimburseApply::$_paidMethod) }}
                        </select>
                    </td>
                    <td style="text-align: right;width: 100px;">
                        <label style="padding-top: 8px;" >佣金账号</label>
                    </td>
                    <td style="text-align: left;">
                        <input type="text" class="form-copy" name="paid_account_no_4" placeholder="佣金账号">
                    </td>
                </tr>
                </tbody>
            </table>
        </div>
    </div>
</div>
<div class="box-footer">
    <div class="col-md-4"></div>
    {{ csrf_field() }}
    <button type="button" id="rac-form" class="btn btn-primary">提交</button>
</div>
<script language="JavaScript" type="text/javascript">
    function checkCodOForm(){
        var paidType = $('#paid_type').val();
        if(paidType == ''){
            swal('返款类型不能为空', '', 'error');
            return false;
        }
        var countryId = $('select[name="country_id"]').val();
        if(countryId == ''){
            swal('国家不能为空', '', 'error');
            return false;
        }
        var feeType = $('#fee_type').val();
        if(feeType == ''){
            swal('费用类型不能为空', '', 'error');
            return false;
        }
        if(feeType == 1 || feeType == 2){
            if(feeType == 1){
                var rType = $('#r_type').val();
                if(rType == ''){
                    swal('刷单类型不能为空', '', 'error');
                    return false;
                }
            }
            var sellerSku = $('input[name="seller_sku"]').val();
            if(sellerSku.trim() == ''){
                swal('亚马逊SKU不能为空', '', 'error');
                return false;
            }
        }
        if(feeType == 4){
            var asin = $('input[name="asin"]').val();
            if(asin.trim() == ''){
                swal('ASIN不能为空', '', 'error');
                return false;
            }
        }
        var currencyCode = $('#currency_code').val();
        if(currencyCode == ''){
            swal('币种不能为空', '', 'error');
            return false;
        }
        var paidMethod = $('#paid_method').val();
        if(paidMethod == ''){
            swal('支付方式不能为空', '', 'error');
            return false;
        }
        var paidAccountNo = $('input[name="paid_account_no"]').val();
        if(paidMethod != 5 && paidAccountNo.trim() == ''){
            swal('支付账号不能为空', '', 'error');
            return false;
        }
        if(paidType == 2){
            var orderAtten = $('input[name="order_atten"]').val();
            if(orderAtten.trim() == ''){
                swal('多次返款订单联系人不能为空', '', 'error');
                return false;
            }
        }
        if(paidMethod == 5){
            if(paidType == 1 || paidType == 3){
                var paidAmount_1 = $('input[name="paid_amount_1"]').val();
                if(paidAmount_1.trim() == ''){
                    swal('一次性付款不能为空', '', 'error');
                    return false;
                }
                if(paidType == 3){
                    var paidAmount_4 = $('input[name="paid_amount_4"]').val();
                    if(paidAmount_4.trim() == ''){
                        swal('佣金不能为空', '', 'error');
                        return false;
                    }
                    var currencyCode_4 = $('#currency_code_4').val();
                    if(currencyCode_4 == ''){
                        swal('佣金币种不能为空', '', 'error');
                        return false;
                    }
                }
            }
            if(paidType == 2){
                var paidAmount_2 = $('input[name="paid_amount_2"]').val();
                if(paidAmount_2.trim() == ''){
                    swal('第一次付款不能为空', '', 'error');
                    return false;
                }
                var paidAmount_3 = $('input[name="paid_amount_3"]').val();
                if(paidAmount_3.trim() == ''){
                    swal('第二次付款不能为空', '', 'error');
                    return false;
                }
            }
        }
        return true;
    }

    $(function () {
        $('#paid_type').select2({placeholder: '返款类型', allowClear: true, width: '200px'});
        $('#country_id').select2({placeholder: '国家', allowClear: true, width: '200px'});
        $('#fee_type').select2({placeholder: '费用类型', allowClear: true, width: '200px'});
        $('#r_type').select2({placeholder: '刷单类型', allowClear: true, width: '200px'});
        $('#currency_code').select2({placeholder: '币种', allowClear: true, width: '200px'});
        $('#paid_method').select2({placeholder: '支付方式', allowClear: true, width: '200px'});
        $('#currency_code_4').select2({placeholder: '币种', allowClear: true, width: '200px'});
        $('#paid_method_4').select2({placeholder: '支付方式', allowClear: true, width: '200px'});
        $(".paid_amount_").inputmask("decimal",
            {
                digits: 3,
                onKeyValidation: function () {
                    if($(this).val() < 0){
                        $(this).val('');
                    }
                }
            }
        );
        var getParams = function () {
            var params = {};
            var apply = {};
            apply['fee_type'] = $('#fee_type').val();
            apply['r_type'] = $('#r_type').val();
            apply['order_no'] = $('input[name="order_no"]').val().trim();
            apply['country_id'] = $('#country_id').val();
            apply['seller_sku'] = $('input[name="seller_sku"]').val().trim();
            apply['asin'] = $('input[name="asin"]').val().trim();
            var paidMethod = $('#paid_method').val();
            apply['paid_method'] = paidMethod;
            apply['paid_account_no'] = $('input[name="paid_account_no"]').val().trim();
            var paidType = $('#paid_type').val();
            apply['paid_type'] = paidType;
            apply['currency_code'] = $('#currency_code').val();
            apply['order_atten'] = $('input[name="order_atten"]').val().trim();
            apply['remarks'] = $('input[name="remarks"]').val().trim();
            var paid = {};
            var _paid_1,_paid_2,_paid_3,_paid_4;
            if(paidType == 1){
                _paid_1 = {};
                _paid_1['paid_amount'] = $('input[name="paid_amount_1"]').val().trim();
                paid[1] = _paid_1;
            }else if(paidType == 2){
                _paid_2 = {};
                _paid_2['paid_amount'] = $('input[name="paid_amount_2"]').val().trim();
                paid[2] = _paid_2;
                _paid_3 = {};
                _paid_3['paid_amount'] = $('input[name="paid_amount_3"]').val().trim();
                paid[3] = _paid_3;
            }else{
                _paid_1 = {};
                _paid_1['paid_amount'] = $('input[name="paid_amount_1"]').val().trim();
                paid[1] = _paid_1;
                _paid_4 = {};
                _paid_4['paid_amount'] = $('input[name="paid_amount_4"]').val().trim();
                _paid_4['paid_method'] = paidMethod == 5 ? 5 : $('#paid_method_4').val();
                _paid_4['paid_account_no'] = $('input[name="paid_account_no_4"]').val().trim();
                _paid_4['currency_code'] = $('#currency_code_4').val();
                paid[4] = _paid_4;
            }
            params.apply = apply;
            params.paid = paid;
            return params;
        };
        $('#rac-form').on('click', function(e) {
            e.preventDefault();
            if(checkCodOForm()){
                var params = getParams();
                $.ajax({
                    method: 'post',
                    url: '/xqc/reimburse/apply-ajax',
                    data: {
                        _token:"{{ csrf_token() }}",
                        action:'add',
                        param:params
                    },
                    success: function (data) {
                        if (typeof data === 'object') {
                            if (data.status) {
                                toastr.success('提交成功 !');
                            } else {
                                swal(data.message, '', 'error');
                            }
                        }
                    }
                });
            }
        });
    });
</script>
